package org.xin.framework;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.xin.common.util.XinUtil;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author ：xin
 * @date ：Created in 2021/7/12 16:12
 * @description：主程序载入点
 * @modified By：
 * @version: v1.0$
 */

@Slf4j
public class Run extends SpringBootServletInitializer {
    private Class<?> appc;

    public static <T> void run(Class<?> cla)  {
        ConfigurableApplicationContext application = SpringApplication.run(cla);
        try {
            logInfo(application);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        new Run().appc = cla;
    }

    //实现configure方法
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(appc);
    }

    //启动成功
    private static void logInfo(ConfigurableApplicationContext application) throws UnknownHostException {
        Environment env = application.getEnvironment();
        //获取配置的端口
        String contextPath = env.getProperty("server.servlet.context-path");
        //IPUtil.getServerIp()获取本机IP，本机使用Hutool工具
        String port = env.getProperty("server.port");
        log.info("\n" +
                "//                          _ooOoo_                               //\n" +
                "//                         o8888888o                              //\n" +
                "//                         88\" . \"88                              //\n" +
                "//                         (| ^_^ |)                              //\n" +
                "//                         O\\  =  /O                              //\n" +
                "//                      ____/`---'\\____                           //\n" +
                "//                    .'  \\\\|     |//  `.                         //\n" +
                "//                   /  \\\\|||  :  |||//  \\                        //\n" +
                "//                  /  _||||| -:- |||||-  \\                       //\n" +
                "//                  |   | \\\\\\  -  /// |   |                       //\n" +
                "//                  | \\_|  ''\\---/''  |   |                       //\n" +
                "//                  \\  .-\\__  `-`  ___/-. /                       //\n" +
                "//                ___`. .'  /--.--\\  `. . ___                     //\n" +
                "//              .\"\" '<  `.___\\_<|>_/___.'  >'\"\".                  //\n" +
                "//            | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |                 //\n" +
                "//            \\  \\ `-.   \\_ __\\ /__ _/   .-` /  /                 //\n" +
                "//      ========`-.____`-.___\\_____/___.-`____.-'========         //\n" +
                "//                           `=---='                              //\n" +
                "//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //\n" +
                "//             佛祖保佑          永无BUG         永不修改         //\n" +
                "\n");
        if (XinUtil.isNull(contextPath)) {
            contextPath = "";
        }
        log.info("\n\t----------------------------------------------------------\n\t" +
                "Application Boot is running! Access URLs:\n\t" +
                "API 文档: \n\t" +
                "\thttp://"+ "127.0.0.1" +':' + port + contextPath + "/\n\t" +
                "\thttp://"+ "127.0.0.1" +':' + port + contextPath + "/doc.html#\n\t" +
                "\thttp://"+ InetAddress.getLocalHost().getHostAddress() +':' + port  + "/doc.html#\n\t" +
                "----------------------------------------------------------"
        );
    }
}
